package com.tbit.uqbike.webmanager.controller;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.tbit.uqbike.object.pojo.dto.SatisfactionDTO;
import com.tbit.uqbike.object.pojo.vo.SatisfactionVO;
import com.tbit.uqbike.object.pojo.vo.StatisticsVO;
import com.tbit.common.entity.view.PageResult;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.service.business.SatisfactionService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * 满意度调查管理
 *
 * @author <a href="mailto:liu.haoxiang@tbit.com.cn">BUGXiang</a>
 * @version 1.0
 * @since 2024/5/10
 */
@RestController
@RequestMapping("/satisfaction")
public class SatisfactionController {

    @Resource
    private SatisfactionService satisfactionService;

    /**
     * 分页查询满意度调查
     *
     * @param satisfactionDTO satisfactionDTO
     * @return result
     */
    @PostMapping("/queryPage")
    public Result<PageResult<SatisfactionVO>> queryPage(@RequestBody @Valid SatisfactionDTO satisfactionDTO) {
        if (Objects.isNull(satisfactionDTO.getPage()) || Objects.isNull(satisfactionDTO.getSize())) {
            return Result.error("分页参数为空");
        }
        if (DateUtil.between(satisfactionDTO.getStartTime(),satisfactionDTO.getEndTime(), DateUnit.DAY) > 30){
            return Result.error("开始时间和结束时间差不能大于一个月");
        }
        return satisfactionService.queryPage(satisfactionDTO);
    }

    /**
     * 满意度调查导出
     *
     * @param satisfactionDTO satisfactionDTO
     * @return result
     */
    @PostMapping("/export")
    public Result<Object> export(@RequestBody @Valid SatisfactionDTO satisfactionDTO) {
        if (DateUtil.between(satisfactionDTO.getStartTime(),satisfactionDTO.getEndTime(), DateUnit.DAY) > 30){
            return Result.error("开始时间和结束时间差不能大于一个月");
        }
        return satisfactionService.export(satisfactionDTO);
    }

    /**
     * 满意度统计
     * @param satisfactionDTO satisfactionDTO
     * @return result
     */
    @PostMapping("/statistics")
    public Result<List<StatisticsVO>> statistics(@RequestBody @Valid SatisfactionDTO satisfactionDTO){
        if (DateUtil.between(satisfactionDTO.getStartTime(),satisfactionDTO.getEndTime(), DateUnit.DAY) > 30){
            return Result.error("开始时间和结束时间差不能大于一个月");
        }
        return satisfactionService.statistics(satisfactionDTO);
    }

}
